package net.hn.hnms.biz.safety.location.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.hn.hnms.biz.api.safety.location.model.IndexTodayVo;
import net.hn.hnms.biz.safety.location.domain.HelmetLampVo;
import net.hn.hnms.biz.safety.location.service.LocationService;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.satoken.config.AutoSetMineCode;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 智呈物联 pan'sx
 * 2022/8/19 11:17
 * 1.0 version
 */
@RestController
@RequestMapping("riskMonitor/location/idnex")
@Api(value = "首页相关接口", tags = {"首页接口"})
@RequiredArgsConstructor
@Slf4j
public class locationIndexController {
    private final LocationService locationService;
    private final RestTemplate restTemplate;

    @GetMapping("todayInfo")
    @ApiOperation("今日信息")
    @AutoSetMineCode
    public R<IndexTodayVo> todayInfo(String mineCode) throws ParseException {
        IndexTodayVo indexTodayVo = locationService.IndexTodayInfo(mineCode);
        return R.ok(indexTodayVo);
    }

    @GetMapping("getStationAddr")
    @ApiOperation("站点位置信息")
    public R<List<Map<String, Object>>> getStationAddr(String mineCode) {

        return R.ok(locationService.getNewStationAddr(mineCode));
    }

    @GetMapping("helmetLamp")
    @ApiOperation("矿灯安全帽")
    public R<ResponsePage<HelmetLampVo>> getTableData(@ApiParam(example = "zx 或lx,是否在线，默认是zx", defaultValue = "zx") String type, int page) {
        try {
            Map forObject = restTemplate.getForObject("http://192.168.220.99:9517/getDevCount", Map.class);
            int count = 0;
            if ("zx".equals(type)) {
                count = Integer.parseInt(forObject.get("devOnline") + "");
            } else {
                count = Integer.parseInt(forObject.get("devOffline") + "");
            }
            String url = "http://192.168.220.99:9517/getDevList?T=" + type + "&s=" + page;
            List<HelmetLampVo> list = new ArrayList<>();
            if (count > 0) {
                HelmetLampVo[] forObject1 = restTemplate.getForObject(url, HelmetLampVo[].class);
                list = Arrays.asList(forObject1);
            }
            return R.ok(new ResponsePage<HelmetLampVo>(list, count));
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return R.fail("矿灯安全帽(192.168.220.99:9517)接口调用失败");
    }

    @GetMapping("helmetLampLoginHis")
    @ApiOperation("矿灯安全帽登录历史")
    public R<Object> getTableData(@ApiParam(name = "使用人") String uuid) {
        try {
            Object forObject = restTemplate.getForObject("http://192.168.220.99:9517/getDevLog?uuid=" + uuid, Object.class);
            return R.ok(forObject);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return R.fail("矿灯安全帽(192.168.220.99:9517)接口调用失败");
    }
}
